/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */
package org.nudt.jCloud.workload;

import java.util.ArrayList;
import java.util.List;

import org.nudt.jCloud.simulation.Request;


public class OrderJob extends LogicalJob{
	
	private int index;
	
	public OrderJob(double arrivalTime, Workload wl){
		super(arrivalTime, wl);
		index = 0;
	}
	
	@Override
	public boolean checkFinished() {
		if(getJobs().size()<=0){
			return true;
		}
		boolean finished = true;
		for(int i=0; i<getJobs().size(); ++i){
			if(getJobs().get(i).isFinished()==false){
				finished = false;
				break;
			}
		}
		return finished;
	}


	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public String type() {
		return "ORDERJOB";
	}

	@Override
	public List<Request> request() {
		List<Request> requests = new ArrayList<Request>();
		if(getJobs().get(index).getUnScheduledTasks().size()>0){
			Request request = new Request(this.getWl().getUser(), this.getJobs().get(index),
					this.getJobs().get(index).getUnScheduledTasks());
			requests.add(request);
		}else if((index+1)<getJobs().size()){
			Request request = new Request(this.getWl().getUser(), this.getJobs().get(index+1),
					this.getJobs().get(index+1).getUnScheduledTasks());
			requests.add(request);
		}
		return requests;
	}
	
}
